package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.base.DataSourceManager;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminJSYYDao;
import jzqb.dao.CGDao;
import jzqb.po.CGPo;
import jzqb.po.JSYYPo;
import jzqb.po.YQTJSYYFLPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 成果库数据层实现类
 */
@Repository
public class AdminJSYYDaoImpl implements AdminJSYYDao{

    @Autowired
    private DataSourceManager kbaseDs;

    @Autowired
    private DataContext dataContext;

    @Value("${filePath.fileTemPResource}")
    private String fileTemPResource;

    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;

    @Value("${filePath.jzyyFilePath}")
    private String jzyyFilePath;

    @Value("${filePath.zjyyCoverPath}")
    private String zjyyCoverPath;

    @Override
    public Boolean updateJSYY(JSYYPo po){
        boolean fileRes = true;
        String newDigitFileName = po.getNewDigitFileName();
        String newCover = po.getNewCover();
        String digitFileName = po.getDigitFileName();
        String cover = po.getCover();
        String delete = po.getNeedDelete();
        String dbAbsPath,tempPath,before= "";
        //1. 把新文件从临时目录移动到数据库，并且删除原文件
        if (!"".equals(newDigitFileName) && newDigitFileName != null){
            if (!"".equals(digitFileName) && digitFileName != null){
                before = FileUtil.calculateAbstractPath(digitFileName,jzyyFilePath);
                FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, before.substring(0,before.lastIndexOf("\\")));
            }
            dbAbsPath = FileUtil.calculateAbstractPath(newDigitFileName,jzyyFilePath);
            tempPath = FileUtil.calculateAbstractPath(newDigitFileName,fileTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            po.setDigitFileName(newDigitFileName);
        }
        // 2. 把新封面从临时目录移动到数据库，并且删除原封面
        if (!"".equals(newCover) && newCover != null){
            if(!"".equals(cover) && cover != null){
                before = FileUtil.calculateAbstractPath(cover,zjyyCoverPath);
                FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, before.substring(0,before.lastIndexOf("\\")));
            }
            dbAbsPath = FileUtil.calculateAbstractPath(newCover,zjyyCoverPath);
            tempPath = FileUtil.calculateAbstractPath(newCover,coverTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            po.setCover(newCover);
        }
        //3. 如果有需要删除的文件则进行删除
        if (!"".equals(delete) && delete != null) {
            before = FileUtil.calculateAbstractPath(delete,jzyyFilePath);
            FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, before.substring(0,before.lastIndexOf("\\")));
        }
        //4. 更新
        String recStr = null;
        recStr = FileuploadUtils.generRECfile("fieldsRemap_jsyy.json","edit", digitFileName,po.getSysID(),po);
        String tableName = SysTool.JSYY_METADATA;
        boolean updateRes = dataContext.updateRecData("SYS_FLD_SYSID", tableName, recStr);
        return updateRes && fileRes;
    }

    @Override
    public Boolean insertJSYY(JSYYPo po){
        String digitFileName = po.getDigitFileName();
        Integer sysID = po.getSysID();
        String cover = po.getCover();
        String dbAbsPath,tempPath = "";
        boolean fileRes = true;
        po.setDigitFileName(digitFileName);
        po.setCover(cover);
        //1 把文件从 临时目录 移动到 数据库
        if (!"".equals(digitFileName)){
            dbAbsPath = FileUtil.calculateAbstractPath(digitFileName,jzyyFilePath);
            tempPath = FileUtil.calculateAbstractPath(digitFileName,fileTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //2 把封面从 临时目录 移动到 数据库
        if (!"".equals(cover)){
            dbAbsPath = FileUtil.calculateAbstractPath(cover,zjyyCoverPath);
            tempPath = FileUtil.calculateAbstractPath(cover,coverTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //3 写入数据到数据库
        String recStr = FileuploadUtils.generRECfile("fieldsRemap_jsyy.json","add",digitFileName, sysID,po);
        String tableName = SysTool.JSYY_METADATA;
        boolean insetRes = dataContext.insertRecData(tableName,recStr);
        return insetRes && fileRes;
    }

    @Override
    public Boolean deleteJSYY(JSYYPo po) {
        //删除数据库数据
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.deleteJSYY";
        return sqlSession.executeDelete(statementId, po.getSysID());
    }

    @Override
    public List<JSYYPo> queryJSYY(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.queryJSYY";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public Long count(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.count";
        return sqlSession.getCount(statementId, bo);
    }

    @Override
    public List<YQTJSYYFLPo> queryJSYYFL(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.queryJSYYFL";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public Boolean insertJSYYFL(YQTJSYYFLPo YQTJSYYFLPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.insertJSYYFL";
        return sqlSession.executeInsert(statementId, YQTJSYYFLPo);
    }

    @Override
    public Boolean deleteJSYYFL(YQTJSYYFLPo YQTJSYYFLPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.deleteJSYYFL";
        return sqlSession.executeDelete(statementId, YQTJSYYFLPo);
    }

    @Override
    public Boolean updateJSYYFL(YQTJSYYFLPo YQTJSYYFLPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.updateJSYYFL";
        return   sqlSession.executeUpdate(statementId, YQTJSYYFLPo);
    }

    @Override
    public List<YQTJSYYFLPo> queryJSYYFLMax() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.queryJSYYFLMax";
        return sqlSession.selectList(statementId, null, false);
    }

    @Override
    public Boolean updateJSYYByDelFl(JSYYPo po) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminJSYYDao.updateJSYYByDelFl";
        return   sqlSession.executeUpdate(statementId, po);
    }

}
